package cn.afactory.tygz.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.harwkin.nb.camera.BaseImgView;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.util.ArrayList;
import java.util.List;

import cn.afactory.tygz.R;
import cn.afactory.tygz.model.SecondGoodsInfo;
import cn.afactory.tygz.ui.base.utils.StringUtils;

/**
 *用户列表适配器
 */
public class SecondGoodsListAdapter extends BaseAdapter {
    private List<SecondGoodsInfo> mList = new ArrayList<SecondGoodsInfo>();
    private Context mContext;

    public SecondGoodsListAdapter(Context ctx, List<SecondGoodsInfo> list) {
        mList = list;
        mContext = ctx;
    }

    @Override
    public int getCount() {
        return mList != null ? mList.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return mList != null ? mList.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        SecondGoodsInfo value = mList.get(position);
        if(convertView == null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_second_goods_list,null);

            holder.iv_goods_image = (ImageView)convertView.findViewById(R.id.iv_goods_image);
            holder.tv_goods_distance = (TextView)convertView.findViewById(R.id.tv_goods_distance);
            holder.tv_goods_title = (TextView)convertView.findViewById(R.id.tv_goods_title);
            holder.tv_goods_cs = (TextView)convertView.findViewById(R.id.tv_goods_cs);
            holder.tv_goods_price = (TextView)convertView.findViewById(R.id.tv_goods_price);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }

        if (value.iconPath != null) {
            BaseImgView.loadimg(holder.iv_goods_image, value.iconPath, R.drawable.ic_pic_loading_default_small,
                    R.drawable.ic_pic_loading_default_small, R.drawable.ic_pic_loading_default_small, ImageScaleType.EXACTLY,
                    (int) mContext.getResources().getDimension(R.dimen.healthcircle_padding30),
                    (int) mContext.getResources().getDimension(R.dimen.healthcircle_padding30),
                    0);
        } else {
            holder.iv_goods_image.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_pic_loading_default_small));
        }


        if(value.name != null){
            holder.tv_goods_title.setText(value.name);
        }

        holder.tv_goods_cs.setText(String.format(mContext.getString(R.string.label_unit_cs),getOldString(value.old)));

        holder.tv_goods_price.setText(String.format(mContext.getString(R.string.label_unit_worth),value.price));

        holder.tv_goods_distance.setText(String.format(mContext.getString(R.string.label_unit_distance),
                StringUtils.formartDistance(value.distance)));

        return convertView;
    }

    private String getOldString(int old){
        return mContext.getResources().getStringArray(R.array.second_goods_cs)[old];
    }

    public void setData(List<SecondGoodsInfo> data) {
        this.mList = data;
        notifyDataSetChanged();
    }

    static class ViewHolder{
        public ImageView iv_goods_image;
        public TextView tv_goods_distance;
        public TextView tv_goods_title;
        public TextView tv_goods_cs;
        public TextView tv_goods_price;
    }
}